package com.library.controller;

import com.library.bean.User;
import com.library.service.UserService;
import com.library.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserService us = new UserServiceImpl();
        String user_account = req.getParameter("userAccount");
        String pass_word = req.getParameter("passWord");
        String user_name = req.getParameter("realName");
        User user = new User(user_account, pass_word, user_name);
        int i = us.user$register(user);
        if(i == 1){
            req.setAttribute("msg", "注册成功");
            resp.sendRedirect("/login.jsp");
        }else{
            req.setAttribute("msg", "注册失败");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }

    }
}
